package com.tjdr.his.drugstore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tjdr.his.common.result.HttpResp;
import com.tjdr.his.common.result.ResultCode;
import com.tjdr.his.domain.entity.GiveBackRecords;
import com.tjdr.his.drugstore.service.IGiveBackRecordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @AUTHOR 张三
 * @DATE 2024/10/21 9:33
 * @DESCRIBER
 */
@RestController
@RequestMapping("/api/records")
@Slf4j
public class GiveBackRecordsController {
    @Autowired
    private IGiveBackRecordsService service;

    @GetMapping("getRecords/{pageSize}/{currentPage}")
    public HttpResp getRecords(@PathVariable int pageSize, @PathVariable int currentPage){

         log.debug("{},{}",pageSize,currentPage);
        List list  = service.getGiveBackRecords();

        HttpResp httpResp = HttpResp.createHttpRespInstance(ResultCode.OPTION_SUCCESS,list);
        log.debug("httpResp:{}",httpResp);
        return httpResp;
    }

    /**
     * 交易记录的查询
     * @param map
     * @return
     */
    @PostMapping("getRecordsByProperty")
    public HttpResp getRecordsByProperty(@RequestBody Map<String,String> map ){
        log.debug("{}",map);
        Integer pageSize =Integer.valueOf(map.get("pageSize"));
        Integer currentPage = Integer.valueOf(map.get("currentPage"));
        String date = map.get("date");
        String operatorState =map.get("operatorState");
        String id = map.get("id");
        GiveBackRecords giveBackRecords = new GiveBackRecords();
        //将字符串转换位日期对象
        if(date!=null&&!"".equals(date)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date myDate  = simpleDateFormat.parse(date);
                giveBackRecords.setDate(myDate);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        giveBackRecords.setOperatorState(operatorState);
        giveBackRecords.setId(id);
        log.debug("record:{}",giveBackRecords);
        IPage pageInfo  = service.getGiveBackRecordsByProperty(giveBackRecords,pageSize,currentPage);
        Map resultMap = new HashMap<>();
        resultMap.put("list",pageInfo.getRecords());
        resultMap.put("total",pageInfo.getTotal());
        HttpResp httpResp = HttpResp.createHttpRespInstance(ResultCode.OPTION_SUCCESS,resultMap);
        log.debug("httpResp:{}",httpResp);
        return httpResp;
    }
}
